package com.example.module_service.device

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.path.RouterPath
import com.example.lib_common.view.entity.DeviceEntity
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_service.R
import com.example.module_service.adapter.DeviceAdapter
import com.example.module_service.databinding.ActivityDeviceBinding
import com.example.module_service.viewmodel.ServerViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.PATH_NOTICE_PAGE)
class DeviceActivity : BaseMvvmActivity<ActivityDeviceBinding>() {



    private val mViewModel :ServerViewModel by viewModels()

    val deviceAdapter =DeviceAdapter()

    override fun getLayoutId(): Int {
        return R.layout.activity_device
    }

    override fun initViews() {
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        mDataBinding.rv.adapter=deviceAdapter


//        mDataBinding.mainss.setOnClickListener{
//            startActivity(Intent(this, AddDeviceActivity::class.java))
//        }


        //点击对应的列表传入对应的id
//        mDataBinding.rv.setOnClickListener {
//            ToastUtils.showLong("点击了第${it.id}个设备")
//            ARouter.getInstance().build(RouterPath.PATH_DEVICE_DETAIL_PAGE)
//                .withInt("id",deviceAdapter.data[it.id].id)
//                .navigation()
//        }

        // 不要给RecyclerView设置点击事件，而是在Adapter中处理

// 在Activity/Fragment中给Adapter设置点击监听
        deviceAdapter.setOnItemClickListener { adapter, view, position ->
            val device = adapter.data[position] as DeviceEntity
            ToastUtils.showLong("点击了第${position}个设备")
            ARouter.getInstance().build(RouterPath.PATH_DEVICE_DETAIL_PAGE)
                .withInt("id", device.id)
                .navigation()
        }
        mViewModel.getDevice()

        mViewModel.DeviceSuccess.observe(this){
            deviceAdapter.data.clear()
            deviceAdapter.data.addAll(it)
            deviceAdapter.notifyDataSetChanged()
        }
    }

    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {

    }

}